package cn.xspace.wnx.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.xspace.android.xafcommon.base.BaseActivity;
import com.xspace.android.xafcommon.been.BaseReturnBean;
import com.xspace.android.xafcommon.been.UserAuthorityBean;
import com.xspace.android.xafcommon.util.Constants;
import com.xspace.android.xafcommon.util.LogUtil;
import cn.xspace.library.util.MyInfoUtil;
import com.xspace.android.xafcommon.util.RequestConstant;
import com.xspace.android.xafcommon.view.RoundAngleImageView;

import java.util.HashMap;
import java.util.Map;

import cn.xspace.app.view.TitleBar;
import cn.xspace.wnx.R;
import cn.xspace.wnx.entity.DeptmentBean;

import cn.xspace.library.util.OkHttpManager;
import com.xspace.android.xafcommon.util.T;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by liuchen on 2016/11/9.
 * 机构职能信息页面
 */

public class OrganizationPostInfoActivity extends BaseActivity {

    private TitleBar mTitle_bar;
    private ImageView mImg_logo;
    private TextView mTxt_org_name;
    private TextView mTxt_org_type;
    private TextView mTxt_org_profession;
    private TextView mTxt_org_area;
    private TextView mTxt_org_business_area;
    private TextView mTxt_org_business_area_left;
    private RelativeLayout mRl_org_business_area;
    private String org_id = "";
    private String user_id = "";
    private DeptmentBean mDeptment = new DeptmentBean();
    //暂时不用
    private TextView mTxt_org_direct;
    private TextView mTxt_org_guikou;
    private TextView mTxt_org_platform;

    @Override
    protected void getIntentData() {
        super.getIntentData();
        org_id = getIntent().getStringExtra(RequestConstant.KEY_ORG_ID);
        LogUtil.d(org_id);
    }

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_organization_duty_info);
    }

    @Override
    protected void initFindViewById() {
        super.initFindViewById();
        mTitle_bar = (TitleBar) findViewById(R.id.title_bar);
        mImg_logo = (RoundAngleImageView) findViewById(R.id.img_org_duty_logo);
        mTxt_org_name = (TextView) findViewById(R.id.txt_org_name);
        mTxt_org_type = (TextView) findViewById(R.id.txt_org_type);
        mTxt_org_profession = (TextView) findViewById(R.id.txt_org_profession);
        mTxt_org_area = (TextView) findViewById(R.id.txt_org_area);
        mTxt_org_business_area = (TextView) findViewById(R.id.txt_org_business_area);
        mTxt_org_business_area_left = (TextView) findViewById(R.id.tv_org_business_area_left);
        mRl_org_business_area = (RelativeLayout) findViewById(R.id.rl_org_business_area);
        //以下暂时不需要
        mTxt_org_direct = (TextView) findViewById(R.id.txt_org_direct);
        mTxt_org_guikou = (TextView) findViewById(R.id.txt_org_guikou);
        mTxt_org_platform = (TextView) findViewById(R.id.txt_org_platform);
    }

    @Override
    protected void initData() {
        super.initData();
        user_id = MyInfoUtil.getMyUserid(mContext);
        final Map<String, Object> dataMap = new HashMap<>();
        dataMap.put(RequestConstant.KEY_ORG_ID, org_id);
        dataMap.put(RequestConstant.KEY_USER_ID, user_id);
        registerSubscription(OkHttpManager.getInstance().commonRequest(Constants.QUEY_DEPT_FUNCTION_INFO, dataMap)
                .subscribeOn(Schedulers.io())
                .map(new Func1<BaseReturnBean, DeptmentBean>() {
                    @Override
                    public DeptmentBean call(BaseReturnBean baseReturnBean) {
                        return new Gson().fromJson(baseReturnBean.data, DeptmentBean.class);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<DeptmentBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        T.showShort(mContext, e.getMessage());

                    }

                    @Override
                    public void onNext(DeptmentBean deptmentBean) {
                        mDeptment = deptmentBean;
                        LogUtil.d((deptmentBean == null) ? " null" : "not null");
                        LogUtil.d(deptmentBean.toString());
                        fillView();
                    }
                }));
    }

//    Subscriber<DeptmentBean> deptSub = ;

    private void fillView() {
        if (TextUtils.isEmpty(mDeptment.dept_logo)) {
            mImg_logo.setImageResource(R.drawable.icon_org_logo_default);
        } else {
            Picasso.with(mContext)
                    .load(mDeptment.dept_logo)
                    .placeholder(R.drawable.icon_org_logo_default)
                    .error(R.drawable.icon_org_logo_default)
                    .into(mImg_logo);
        }
        mTxt_org_name.setText(mDeptment.dept_name);
        mTxt_org_type.setText(mDeptment.dept_type);
        mTxt_org_profession.setText(mDeptment.dept_profession);
        mTxt_org_area.setText(mDeptment.dept_area);

        if ("1".equals(mDeptment.bizList_isshow)) {
            mTxt_org_business_area_left.setText(R.string.org_business_area_duty2);
            StringBuffer sbfFunc = new StringBuffer();
            for (UserAuthorityBean funcBean : mDeptment.bizList) {
                sbfFunc.append(funcBean.dict_name + "\n");
            }
            if (sbfFunc.lastIndexOf("\n") != -1) {
                sbfFunc.deleteCharAt(sbfFunc.lastIndexOf("\n"));
            }
            String funcName = sbfFunc.toString();
            LogUtil.d(funcName);
            mTxt_org_business_area.setText(funcName);
        } else if ("1".equals(mDeptment.funList_isshow)) {
            StringBuffer sbfFunc = new StringBuffer();
            for (UserAuthorityBean funcBean : mDeptment.funList) {
                sbfFunc.append(funcBean.function_name + "\n");
            }
            if (sbfFunc.lastIndexOf("\n") != -1) {
                sbfFunc.deleteCharAt(sbfFunc.lastIndexOf("\n"));
            }
            String funcName = sbfFunc.toString();
            LogUtil.d(funcName);
            mTxt_org_business_area.setText(funcName);
        } else if ("0".equals(mDeptment.bizList_isshow) && "0".equals(mDeptment.funList_isshow)) {
            mRl_org_business_area.setVisibility(View.GONE);
        }

        //以下暂时无用
        StringBuffer sbfDirect = new StringBuffer();
        for (DeptmentBean directBean : mDeptment.directList) {
            sbfDirect.append(directBean.dept_name + "\n");
        }
        if (sbfDirect.lastIndexOf("\n") != -1) {
            sbfDirect.deleteCharAt(sbfDirect.lastIndexOf("\n"));
        }
        String directName = sbfDirect.toString();
        LogUtil.d(directName);
        mTxt_org_direct.setText(directName);

        StringBuffer sbfBelong = new StringBuffer();
        for (DeptmentBean belongBean : mDeptment.belongList) {
            sbfBelong.append(belongBean.dept_name + "\n");
        }
        if (sbfBelong.lastIndexOf("\n") != -1) {
            sbfBelong.deleteCharAt(sbfBelong.lastIndexOf("\n"));
        }
        String belongName = sbfBelong.toString();
        LogUtil.d(belongName);
        mTxt_org_guikou.setText(belongName);

        StringBuffer sbfPlat = new StringBuffer();
        for (DeptmentBean platformBean : mDeptment.platList) {
            sbfPlat.append(platformBean.dept_name + "\n");
        }
        if (sbfPlat.lastIndexOf("\n") != -1) {
            sbfPlat.deleteCharAt(sbfPlat.lastIndexOf("\n"));
        }
        String platName = sbfPlat.toString();
        LogUtil.d(platName);
        mTxt_org_platform.setText(platName);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mTitle_bar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onEventMainThread(Map<String, Object> inParam) {
        super.onEventMainThread(inParam);
    }
}
